package com.achilles.netease;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, PlayMode
        .PlayModeChange, MusicPlayManager.MusicStatusChange {

    private ImageView mIvPlay;
    private DiscPaneView mDiscPaneView;
    private PlayMode mPlayMode;

    private void bindViews() {
        mDiscPaneView = (DiscPaneView) findViewById(R.id.discView);
        mIvPlay = (ImageView) findViewById(R.id.ivPlay);
        mIvPlay.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
        initData();
        showSystemUI();
        setBackground();
    }

    public void setBackground() {

    }

    public void showSystemUI() {
        View view = MainActivity.this.getWindow().getDecorView();
        view.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    private void initData() {
        mPlayMode = new PlayMode();
        mPlayMode.setPlayModeChange(this);
        mPlayMode.init();
        MusicPlayManager.getInstance().setMusicStatusChange(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.ivPlay:
                mPlayMode.toggleMode();
                break;

            default:
                break;
        }
    }

    @Override
    public void modeChange(int mode) {
        // TODO Auto-generated method stub
        switch (mode) {
            case PlayMode.MODE_INIT: {
                mIvPlay.setBackgroundResource(R.drawable.play_fm_btn_play_prs);
                break;
            }
            case PlayMode.MODE_PAUSE: {
                pasueMusic();
                break;
            }
            case PlayMode.MODE_PLAYING: {
                playMusic();
                break;
            }
        }
    }

    private void playMusic() {
        // TODO Auto-generated method stub
        mIvPlay.setBackgroundResource(R.drawable.play_fm_btn_pause_prs);
        mDiscPaneView.play();
    }

    private void pasueMusic() {
        // TODO Auto-generated method stub
        mIvPlay.setBackgroundResource(R.drawable.play_fm_btn_play_prs);
        MusicPlayManager.getInstance().pause();
        mDiscPaneView.pasue();
    }

    @Override
    public void complete() {
        mPlayMode.setMode(PlayMode.MODE_PAUSE);
        mIvPlay.setBackgroundResource(R.drawable.play_fm_btn_play_prs);
        mDiscPaneView.pasue();
    }
}
